$(function () {
    var timer = null;
    var pdata = {
        text: 13341895895,
        color: `#` + generateMixed(),
        type: 'right'
    }
    HOME.ajaxHOMEFn({
        url: HOME.baseUrl.videolist,
        type: 'POST',
        dataType: 'json',
        data: {
            goods_id: HOME.getQueryVariable('curId'),
            category_id: HOME.getQueryVariable('id')
        }
    }, function (res) {
        $('#video_box').attr({'data_id' : res[0].id})
        $('#box').html(template('videoListTpl', {'res': res}));
        if ($('#box dl').height() * $('#box dl').length > 506) {
            setSrollBar()
        } else {
            $('#scrollBar').css('display', 'none')
        }
        var windowHeight = $(window).height();
        var mainWrapHeight = $('.video_back').height();
        if(mainWrapHeight < windowHeight - 170){
            $('.video_back').height(windowHeight - 170 + 'px');
        }
        if(res.length <= 0 ) return false
        HOME.ajaxHOMEFn({
            url: HOME.baseUrl.inspect,
            type: 'POST',
            dataType: 'json',
            data: {
                videoId: res[0].id
            }
        }, function (res) {
            $('#box dl').eq(0).addClass('cur')
            var videoBox = new DPlayer({
                container: document.getElementById('video_box'),
                live: false,
                logo: '../images/logo/logo_video.png',
                danmaku:{
                    addition: [],
                },
                video: {
                    //loop: true, // 是否循环播放
                    //pic: "", // 视频封面
                    mutex: true, //互斥，阻止多个播放器同时播放
                    url: res.play_url, // 播放url
                    type: 'customFlv'
                    // quality: options.quality, // 清晰度选择
                    // defaultQuality: 2, // 默认清晰度
                },
                ended: function () {
                   
                }
        
            });
            videoBox.on('ended', function () {
                var nextUrl = $('#dplayer').data('url')
                if (nextUrl) {
                    window.location.href = nextUrl
                }
                clearInterval(timer)
                console.log(timer)
                videoBox.danmaku.draw({
                    text: '',
                    color: '',
                    type: ''
                })
            })
            videoBox.on('canplaythrough', function () {
                console.log('完成了')
                console.log(videoBox.danmaku)
                pdata.color= `#` + generateMixed()
                clearInterval(timer)
                timer = setInterval(function(){
                    videoBox.danmaku.draw(pdata);
                },3000)
            })
            videoBox.on('pause', function () {
                console.log('暂停了')
                console.log(timer)
                clearInterval(timer)
                videoBox.danmaku.draw({
                    text: '',
                    color: '',
                    type: ''
                })
            })
            // videoBox.on('play', function () {
            //     console.log('播放了')
            //     pdata.color= `#` + generateMixed()
            //     timer = setInterval(function(){
            //         videoBox.danmaku.draw(pdata);
            //     },3000)
            // })



        })


    });


    $('body').on('click', '#box dl', function () {
        var dataId = $(this).attr('data_id')
        var _this = $(this)
        HOME.ajaxHOMEFn({
            url: HOME.baseUrl.inspect,
            type: 'POST',
            dataType: 'json',
            data: {
                videoId: dataId
            }
        }, function (res) {
            _this.addClass('cur').siblings().removeClass('cur')
            var videoBox = new DPlayer({
                container: document.getElementById('video_box'),
                live: false,
                video: {
                    //loop: true, // 是否循环播放
                    //pic: "", // 视频封面
                    mutex: true, //互斥，阻止多个播放器同时播放
                    url: res.play_url, // 播放url
                    type: 'customFlv'
                    // quality: options.quality, // 清晰度选择
                    // defaultQuality: 2, // 默认清晰度
                },
                ended: function () {
                    var nextUrl = $('#dplayer').data('url')
                    if (nextUrl) {
                        window.location.href = nextUrl
                    }
                }
            });

        })
    })


    

});

function generateMixed(){
    return ('000000' + Math.floor(Math.random() * 999999)).slice(-6)
}


